<template>
  <div class="musicList">
    <div class="musicList-top">
      <div class="title">发现好歌单</div>
      <div class="more">查看更多</div>
    </div>
    <div class="mlist">
      <div class="swiper-container" id="musicSwiper">
        <div class="swiper-wrapper">
          <router-link
            :to="{ path: './listView', query: { id: item.id } }"
            class="swiper-slide"
            v-for="(item, index) in state.musicList"
            :key="index"
          >
            <img :src="item.picUrl" :alt="item.name" />
            <div class="name">{{ item.name }}</div>
            <div class="count">
              <svg class="icon" aria-hidden="true">
                <use xlink:href="#icon-icon-"></use>
              </svg>
              <span>{{ changeCount(item.playCount) }}</span>
            </div>
          </router-link>
        </div>
      </div>
    </div>
  </div>
</template>


<script>
import "swiper/css/swiper.css";
import Swiper from "swiper";
import * as indexApi from "@/api/index";
import { reactive, onMounted, onUpdated } from "vue";
//vue 2x:
/* export default {
  name: "musicList",
  data() {
    return {
      musicList: [],
    };  
  },
  methods: {
    carouselScroll() {
      var mySwiper = new Swiper("#musicSwiper", {
        slidesPerView: 3,
        spaceBetween: 10,
      });
    },
    changeCount: function (value) {
      console.log(value);
      let result = value.toString();
      if (value >= 100000000) {
        result = `${(value / 100000000).toFixed(2)}亿`;
      } else if (value >= 10000) {
        result = `${(value / 10000).toFixed(2)}万`;
      }

      return result;
    },
  },
  async mounted() {
    let result = await indexApi.personalized({
      limit: 10,
    });
    this.musicList = result.result;
  },

  updated() {
    //更新完成后执行相关函数
    this.carouselScroll();
  },
}; */

//vue3
export default {
  setup() {
    //setup函数是处于 生命周期函数 beforeCreate 和 Created 两个钩子函数之前的函数
    let state = reactive({ musicList: [] }); //此时musicList变量就是响应式的
    function changeCount(value) {
      let result = value.toString();
      if (value >= 100000000) {
        result = `${(value / 100000000).toFixed(2)}亿`;
      } else if (value >= 10000) {
        result = `${(value / 10000).toFixed(2)}万`;
      }

      return result;
    }
    function carouselScroll() {
      var mySwiper = new Swiper("#musicSwiper", {
        slidesPerView: 3,
        spaceBetween: 10,
      });
    }
    onMounted(async () => {
      let result = await indexApi.personalized({
        limit: 10,
      });
      state.musicList = result.result;
    });
    onUpdated(() => {
      carouselScroll();
    });
    return {
      state,
      changeCount,
    };
  },
};
</script>

<style lang="less" scoped>
.musicList {
  width: 7.5rem;
  padding: 0 0.4rem;
  .musicList-top {
    display: flex;
    justify-content: space-between;
    height: 1rem;
    align-items: center;
    .title {
      font-size: 0.4rem;
      font-weight: 900;
    }
    .more {
      border: 1px solid #ccc;
      border-radius: 0.2rem;
      font-size: 0.24rem;
      height: 0.5rem;
      width: 1.3rem;
      text-align: center;
      line-height: 0.5rem;
    }
  }
  .mlist {
    .swiper-container {
      width: 100%;
      height: 2.9rem;
      .swiper-slide {
        display: flex;
        flex-direction: column;
        position: relative;
        img {
          width: 100%;
          height: auto;
          border-radius: 0.1rem;
        }
        .name {
          height: 0.6rem;
          width: 100%;
          font-size: 0.24rem;
          line-height: 0.4rem;
        }
        .count {
          position: absolute;
          right: 0.1rem;
          top: 0.1rem;
          font-size: 0.24rem;
          color: #ccc;
          display: flex;
          align-items: center;
          .icon {
            fill: #ccc;
          }
        }
      }
    }
  }
}
</style>